# -*- coding: utf-8 -*-
from autoscrapy.db import link
from autoscrapy.utils import log
from autoscrapy.utils import basePlugin 


LOG = log.get_logging(__name__)


class SqlLinkfilter(basePlugin.BasePlugin):

    def __init__(self, context):
        self._context = context

    @property
    def session(self):
        return self._context['session']

    def is_duplicate_url(self, url):
        query = self.session.query(link.link).filter(link.link.link == url[:100])
        if query.first():
            return True
        else:
            return False

    def add_url(self, url):
        if not self.is_dumplicate_url(url):
            with self.session.begin(subtransactions=True):
                try:
                    self.session.add(link.link(link=url[:100]))
                    self.session.flush()
                except:
                    LOG.error(" add url %s fail", url)

